<?php
/**
 * 卖家模块
 * @version 1.0
 * @author ezhu <ezhu@jipukeji.com>
 */

namespace Shop\Event;

class SellerEvent{
    
    
    /**
     * 是否已登录
     * @param $isId 是否只返回id
     * @param [type] $type 1/2默认为1 2免密登录
     */
    public function getSellerInfo($field='',$type=1){
        if ($type == 1) {
            $seller_user = session('seller_user');
        }else{
            $seller_user = $this->getInfos();
        }
        
        return !empty($field) ? $seller_user[$field] : $seller_user;
    }
    

    
    /**
     * 卖家登录
     * @param string $user
     * @param string $passwd 
     * @return mixed 登录成功返回true，失败返回字符串状态
     */
    public function login($user, $passwd){

        $seller = D('Seller');

        //取得管理员信息
        $sellerUser = $seller->getInfo($user);

        //管理员不存在
        if(!$sellerUser){
            return '店铺管理员不存在';
        }
        //总平台设置绑定用户 判断登陆用户是否是本商家 
        if (C('STORE_IS_BIND')) {
            $user_info = $_SESSION['jipushop_home']['user_auth'];
            if ($sellerUser['member_id'] != $user_info['uid']) {
                return '登陆用户和商家账号不匹配！';
            }
        }
        
        // 查询店铺状态
        $storeMap['status'] = 1;
        $storeMap['id'] = $sellerUser['store_id'];
        $store = M('Store')->where($storeMap)->find();
        if(empty($store)){
            return '店铺不存在';
        }

        //管理员登录密码错误
        if($sellerUser['member_id']){//$sellerUser['seller_passwd'] != $seller->passwdEncrypt($passwd)
            //查找用户模块中的用户信息
            $userMap['id'] = array('eq',$sellerUser['member_id']);
            $userMap['status'] = array('gt',0);
            $userInfo = M('User')->where($userMap)->find();
            if(empty($userInfo)){
                return '卖家不存在';
            }
            if(think_ucenter_md5($passwd) !== $userInfo['password'] && $userInfo['from'] != 'ts'){
                return '密码错误';
            }else{
                $ts_user = D('ts_user')->where(array('uid'=>$sellerUser['member_id']))->find();
                if( is_array($ts_user) && $ts_user['status'] == 1 && md5(md5($passwd).$ts_user['login_salt']) != $ts_user['password']) {
                  return '密码错误';
                }
            }
        }else{
            return '商家用户不存在';
        }
        //将上次登陆时间重新处理 buddha 20170503
        $sellerUser['old_last_login_time'] = $sellerUser['last_login_time'];
        //更新登录信息
        $array = array(
            'last_login_time'  => NOW_TIME
        );
        $seller->where(array('seller_id'=>$sellerUser['seller_id']))->save($array);
        //添加卖家店铺营销活动列表
        $grade_id = M('Store')->where(array('store_id'=>$sellerUser['store_id']))->getField('grade_id');
        $array['marketing'] = M('StoreGrade')->where(array('id'=>$grade_id))->getField('marketing');
        
        $array['login_ip']   =  get_client_ip();
        $array['login_time'] = NOW_TIME;
        $sellerUser = array_merge($sellerUser, $array);

        //设置登录信息
        session('seller_user',null); // 清空session
        session('seller_user', $sellerUser);
        return true;
    }

    /**
     * 商户免登陆 操作
     * @author buddha <buddha@jipu.com>
     * @return [type] [description]
     */
    public function getInfos(){
        //前端登录用户session
        $user = $_SESSION['jipushop_home']['user_auth'];

        //登录超时
        if (!$user['uid']) {
            return '登录超时，请重新登录';
        }

        $user_name = M('Seller')->where('member_id='.$user['uid'])->getField('seller_name');

        $seller = D('Seller');

       //取得管理员信息
        $sellerUser = $seller->getInfo($user_name);

       //管理员不存在
        if(!$sellerUser)return '店铺不存在';

        // 查询店铺状态
        $storeMap['status'] = 1;
        $storeMap['id'] = $sellerUser['store_id'];
        $store = M('Store')->where($storeMap)->find();
        if(empty($store))return '店铺不存在';

        //将上次登陆时间重新处理 buddha 20170503
        $sellerUser['old_last_login_time'] = $sellerUser['last_login_time'];
        //更新登录信息
        $array = array(
            'last_login_time'  => NOW_TIME
        );
        $seller->where(array('seller_id'=>$sellerUser['seller_id']))->save($array);
        //添加卖家店铺营销活动列表
        $grade_id = M('Store')->where(array('store_id'=>$sellerUser['store_id']))->getField('grade_id');
        $array['marketing'] = M('StoreGrade')->where(array('id'=>$grade_id))->getField('marketing');
        
        $array['login_ip']   =  get_client_ip();
        $array['login_time'] = NOW_TIME;
        $sellerUser = array_merge($sellerUser, $array);

        //设置登录信息
        session('seller_user',null); // 清空session
        session('seller_user', $sellerUser);
        return $sellerUser;
    }
}
